﻿<?php

require 'config_bd.php';
require 'cle_encrypt.php';

$uname = $_REQUEST["name"];
$upass = $_REQUEST["thePass"];
$pdate = $_REQUEST["theDate"];

$sql =        " select a.week_day, a.cdate, a.Name, a.week_day_client_hours from ( ";
$sql = $sql . " select s.week_day, s.cdate, s.Name, sum(s.hours) as week_day_client_hours ";
$sql = $sql . "  from summary_base_vw s ";
$sql = $sql . "  where s.user_id = get_user_id( :uname, AES_ENCRYPT(:upass ,:cle_encrypt) ) ";
$sql = $sql . " group by s.week_day,s.cdate,s.Name ";
$sql = $sql . " having sum(s.hours) != 0 ";
$sql = $sql . " UNION ";
$sql = $sql . " select '',s.cdate,'Total', sum(s.hours) ";
$sql = $sql . "  from summary_base_vw s ";
$sql = $sql . "  where s.user_id = get_user_id ( :uname, AES_ENCRYPT(:upass ,:cle_encrypt))  ";
$sql = $sql . "  group by s.week_day,s.cdate ) a ";
$sql = $sql . " where a.cdate = :pdate ";

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$stmt = $dbh->prepare($sql);  
	$stmt->bindParam("uname", $uname);
	$stmt->bindParam("upass", $upass);
	$stmt->bindParam("cle_encrypt", $cle_encrypt);
	$stmt->bindParam("pdate", $pdate);
   
	$stmt->execute();
	
	$alldata = $stmt->fetchAll(PDO::FETCH_OBJ);
	$dbh = null;
	echo '{"items":'. json_encode($alldata) .'}'; 

} catch(PDOException $e) {
	echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

?>